package com.cwt_taxi_xa.util;

/**
 * Created by ZQX on 2019/9/5 17:48
 */
public class IntUtil {
    // 大端模式---------------------------------------------------------------
    public static byte[] bigInt16ToBytes(int value) {
        byte[] src = new byte[2];
        src[0] = (byte) ((value >> 8) & 0xFF);
        src[1] = (byte) (value & 0xFF);
        return src;
    }

    public static byte[] bigInt32ToBytes(int value) {
        byte[] src = new byte[4];
        src[0] = (byte) ((value >> 24) & 0xFF000000);
        src[1] = (byte) ((value >> 16) & 0xFF0000);
        src[2] = (byte) ((value >> 8) & 0xFF00);
        src[3] = (byte) (value & 0xFF);
        return src;
    }

    // 小端模式---------------------------------------------------------------
    public static int littleBytesToInt32(byte[] buffer) {
        int num = buffer[0] & 0xFF;
        num |= ((buffer[1] << 8) & 0xFF00);
        num |= ((buffer[2] << 16) & 0xFF0000);
        num |= ((buffer[3] << 24) & 0xFF000000);
        return num;
    }

    public static int littleBytesToInt16(byte[] buffer) {
        int num = buffer[0] & 0xFF;
        num |= ((buffer[1] << 8) & 0xFF00);
        return num;
    }
}
